package com.example.shiro.model.sys.controller;

import com.example.shiro.common.response.R;
import com.example.shiro.common.util.JwtUtil;
import com.example.shiro.common.util.MD5Utils;
import com.example.shiro.model.sys.entity.UserEntity;
import com.example.shiro.model.sys.entity.dto.UserInfoDto;
import com.example.shiro.model.sys.mapper.UserMapper;
import com.example.shiro.model.sys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.Objects;

/**
 * @auther 严旭平
 * @date 2020/4/2317:44
 * @Description
 */
@Validated
@RestController
@Api(description = "登录接口")
public class LoginController {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class );

    @Autowired
    private UserService userService;

    @ApiOperation("采用用户名密码进行登录")

    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "登录账号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "password", value = " 登录密码", required = true, dataType = "string", paramType = "query")
    })
    @GetMapping("/login")
    @RequestMapping(value = "/login",method = {RequestMethod.GET,RequestMethod.POST})
    public R login( @RequestParam(value = "username") @NotBlank(message = "登录账号不能为空")  String username
                   ,@NotBlank(message = "登录密码不能为空") @RequestParam(value = "password") String password ){

//        UserInfoDto userInfoByUsername = userService.getUserInfoByUsername(username);
        UserEntity userByUsername = userService.getUserByUsername(username);
        if(Objects.isNull( userByUsername )){

            return R.error().msg("用户名不存在");
        }

        if( !MD5Utils.verify( password , userByUsername.getPassword() )){
            return R.error().msg("密码不正确");
        }

        String sign = JwtUtil.sign( username , MD5Utils.getSaltMD5( password ) );

        return R.ok().data( "token", sign );
    }

    /**
     * 推出系统
     * @return
     */
    @GetMapping("/loginOut")
    public R loginOut(){
        return R.ok().msg("退出成功");
    }


    /**
     * 添加用户权限测试
     * @return
     */
    @RequiresPermissions(value = "user:add")
    @GetMapping("userAdd")
    public R userAdd(){
        return R.ok().msg("执行成功");
    }


    /**
     * 删除用户权限设置
     * @return
     */
    @GetMapping("userDelete")
    @RequiresPermissions(value = "user:delete")
    public R userDelete(){
        return R.ok().msg("执行成功");
    }

}
